/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.web.misc.jms;

import com.google.common.collect.ImmutableMap;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Destination;

/**
 * Created on 3/24/14.
 *
 * @author vacoor
 */
public class NotifyMessageProducer {
    private JmsTemplate jmsTemplate;
    private Destination notifyQueue;
    private Destination notifyTopic;

    public void sendQueue(String message) {
        sendMessage(message, notifyQueue);
    }

    public void sendTopic(String message) {
        sendMessage(message, notifyTopic);
    }

    private void sendMessage(String message, Destination destination) {
        jmsTemplate.convertAndSend(destination, message);
    }

    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public Destination getNotifyQueue() {
        return notifyQueue;
    }

    public void setNotifyQueue(Destination notifyQueue) {
        this.notifyQueue = notifyQueue;
    }

    public Destination getNotifyTopic() {
        return notifyTopic;
    }

    public void setNotifyTopic(Destination notifyTopic) {
        this.notifyTopic = notifyTopic;
    }

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("etc/spring-jms.xml");
        NotifyMessageProducer producer = context.getBean(NotifyMessageProducer.class);

        producer.jmsTemplate.convertAndSend(producer.notifyTopic, ImmutableMap.<String, Object>of("hehe", "haha"));
    }
}
